package com.whty.system.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;



public class AESCoder {
	
	 /**
     * 加密
     * hexStr和hexKey都须为16进制表示的字符串
     * 加密后返回16进制表示的字符串*/
    public static String ecbEnc(String hexStr, String hexKey){
        String rs=null;
        try {
            byte[] inBytes = HexUtil.hexToBytes(hexStr);            
            byte[] keyBytes = HexUtil.hexToBytes(hexKey);
            SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");// "算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(inBytes);            
            rs=HexUtil.bytesToHex(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
          
        }
        return rs;
    }
    
    public static String cbcEnc(String hexStr, String hexKey){
        String rs=null;
        try {
            byte[] inBytes = HexUtil.hexToBytes(hexStr);            
            byte[] keyBytes = HexUtil.hexToBytes(hexKey);
            SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");// "算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(inBytes);            
            rs=HexUtil.bytesToHex(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
          
        }
        return rs;
    }
    
    public static String cbcEnc(String hexStr, String hexKey,String iv){
        String rs=null;
        try {
            byte[] inBytes = HexUtil.hexToBytes(hexStr);            
            byte[] keyBytes = HexUtil.hexToBytes(hexKey);
            SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");// "算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec,new IvParameterSpec(CommonMethods.stringToBytes(iv)));
            byte[] encrypted = cipher.doFinal(inBytes);            
            rs=HexUtil.bytesToHex(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
          
        }
        return rs;
    }
    
    /**
     * 解密
     * hexStr和hexKey都须为16进制
     * 加密后返回16进制的字符串*/
    public static String ecbDec(String hexStr,String hexKey){
        String rs=null;
        try {
            byte[] outBytes = HexUtil.hexToBytes(hexStr);
            byte[] keyBytes = HexUtil.hexToBytes(hexKey);
            SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");// "算法/模式/补码方式"
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] decBytes = cipher.doFinal(outBytes);
            rs=HexUtil.bytesToHex(decBytes);
        } catch (Exception e) {
        	e.printStackTrace();
//            log.error("解密异常",e);
//            log.error("输入参数为hexStr：{},hexKey:{}",hexStr,hexKey);
        }
        return rs;
    }
    
    
    
//    /**
//     * 解密
//     * hexStr和hexKey都须为16进制
//     * 加密后返回16进制的字符串*/
//    public static String cbcDec(String hexStr,String hexKey,byte[] iv){
//        String rs=null;
//        try {
//            byte[] outBytes = HexUtil.hexToBytes(hexStr);
//            byte[] keyBytes = HexUtil.hexToBytes(hexKey);
//            SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
//            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");// "算法/模式/补码方式"
//            cipher.init(Cipher.DECRYPT_MODE, skeySpec,new IvParameterSpec(iv));
//            byte[] decBytes = cipher.doFinal(outBytes);
//            rs=HexUtil.bytesToHex(decBytes);
//        } catch (Exception e) {
//        	e.printStackTrace();
////            log.error("解密异常",e);
////            log.error("输入参数为hexStr：{},hexKey:{}",hexStr,hexKey);
//        }
//        return rs;
//    }
    
    public static String cbcDec(String hexStr,String hexKey,String  iv){
        String rs=null;
        try {
            byte[] outBytes = HexUtil.hexToBytes(hexStr);
            byte[] keyBytes = HexUtil.hexToBytes(hexKey);
            SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");// "算法/模式/补码方式"
            cipher.init(Cipher.DECRYPT_MODE, skeySpec,new IvParameterSpec(CommonMethods.stringToBytes(iv)));
            byte[] decBytes = cipher.doFinal(outBytes);
            rs=HexUtil.bytesToHex(decBytes);
        } catch (Exception e) {
        	e.printStackTrace();
//            log.error("解密异常",e);
//            log.error("输入参数为hexStr：{},hexKey:{}",hexStr,hexKey);
        }
        return rs;
    }
    public static String mycbcDec(String hexStr,String hexKey)
    {
//    	String a=ecbDec(hexStr,hexKey);
//    	String b=cbcDec(hexStr,hexKey,"0000000000000000".getBytes());
//    	return a.substring(0, 32)+b.substring(32);
    	return null;
    }

    public static void main(String args[])
    {
//    	String a=ecbDec("50340252D9D7A01CD8FCEC315D7438AC62356B5693D6A57C9A4ECE6C2FE7BDD1","00000000000000000000000000000000");
//    	String b=cbcDec("50340252D9D7A01CD8FCEC315D7438AC62356B5693D6A57C9A4ECE6C2FE7BDD1","00000000000000000000000000000000","0000000000000000".getBytes());
//    	System.out.println(a.substring(0, 32)+b.substring(32));
    	//460C80AD2DA8E9665D303B1651D07E4146B5479C81EF406F946A5500655CB8EA
//    	String data="3137A87A8222844D196CBD9B8180E349f3216b941d373b6878dde3f080cd3a46";
    	String key="00000000000000000000000000000000";
//    	System.out.println(cbcEnc(data, key,"00000000000000000000000000000000"));
    	System.out.println(cbcDec("60340252D9D7A01CD8FCEC315D7438AC62356B5693D6A57C9A4ECE6C2FE7BDD1",key,"00000000000000000000000000000000"));
    }

}
